package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.UserAttribute;
import com.csap.framework.oauth2.server.entity.UserAttributeEntity;
import com.csap.framework.oauth2.server.model.UserAttributeModel;
import com.csap.framework.oauth2.server.mybatis.UserAttributeMapper;
import com.csap.framework.oauth2.server.service.UserAttributeService;
import com.csap.framework.util.CollectionsUtil;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户属性
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@Service
@Description(value = "用户属性ServiceImpl")
public class UserAttributeServiceImpl extends BaseCsapServiceImpl<UserAttributeMapper, UserAttributeEntity, UserAttributeModel, String> implements UserAttributeService {
    @Override
    public Map<String, List<String>> userAttribute(String userId) {
        return lambdaQuery()
                .select(UserAttributeEntity::getName, UserAttributeEntity::getValue)
                .eq(UserAttributeEntity::getUserId, userId)
                .listOptional()
                .orElseGet(Collections::emptyList)
                .stream()
                .collect(Collectors.toMap(UserAttributeEntity::getName, UserAttributeEntity::getValue));
    }

    @Override
    public boolean insert(UserAttributeModel t) {
        if (Objects.isNull(t.getValue())) {
            t.setValue(Lists.newArrayList());
        }
        return lambdaQuery()
                .eq(UserAttributeEntity::getUserId, t.getUserId())
                .eq(UserAttributeEntity::getName, t.getName())
                .eq(UserAttributeEntity::getOpenId, t.getOpenId())
                .select(UserAttributeEntity::getAttributeId)
                .oneOptional()
                .when(Objects::isNull, i2 -> super.insert(t), i2 -> {
                    return super.update(UserAttributeModel.builder()
                            .attributeId(i2.getAttributeId())
                            .value(t.getValue())
                            .build());
                });
    }

    @Override
    public Boolean removeByName(String name, String userId, String openId) {
        return lambdaUpdate()
                .eq(UserAttributeEntity::getName, name)
                .eq(UserAttributeEntity::getUserId, userId)
                .eq(UserAttributeEntity::getOpenId, openId)
                .remove();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Boolean updateAttribute(List<UserAttribute> userAttributeList, String openId, String userId) {
        if (CollectionsUtil.isEmpty(userAttributeList)) {
            return Boolean.FALSE;
        }
        UserAttributeModel userAttributeModel = new UserAttributeModel();
        userAttributeModel.setUserId(userId);
        userAttributeModel.setOpenId(openId);
        userAttributeList.forEach(i -> {
            userAttributeModel.setName(i.getName());
            userAttributeModel.setValue(i.getValue());
            userAttributeModel.setAttributeId(null);
            insert(userAttributeModel);
        });
        return Boolean.TRUE;
    }

}
